package controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Utils {
	
	public static Object desserializa(String idxFile) {
		Object o;
		try {
			ObjectInputStream inputter = new ObjectInputStream(new FileInputStream(idxFile));
			o = inputter.readObject();
			return o;
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(
					"Classe nao encontrada ao desserializar objeto", e);
		} catch (IOException e) {
			throw new RuntimeException("Erro de E/S ao desserializar objeto", e);
		}
	}
	
	public static String getFileAsString(File arquivo) {
		BufferedReader isr;
		try {
			isr = new BufferedReader(new FileReader(arquivo));
		} catch (FileNotFoundException e) {
			System.out.println("Erro ao abrir arquivo: " + arquivo);
			throw new RuntimeException(e);
		}
		
		
		String estringao;
		try {
			StringBuffer sb = new StringBuffer();
			
			char[] buff = new char[1024];
			int numRead;
			while ((numRead = isr.read(buff) ) != -1) {
				sb.append(String.valueOf(buff, 0, numRead));
			}
			estringao = sb.toString();
			
		} catch (IOException e) {
			throw new RuntimeException("Erro ao ler arquivo: "+arquivo);
		}
		return estringao;
	}

}
